
package cz.czu.pef.DataModelling.ejb.impl;

import java.util.logging.*;

import javax.ejb.*;
import javax.interceptor.Interceptors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ejb.interceptor.SpringBeanAutowiringInterceptor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import cz.czu.pef.DataModelling.*;
import cz.czu.pef.DataModelling.dao.*;
import cz.czu.pef.DataModelling.ejb.api.*;

@Service(value="addressService")
// ##################################################
// JBOSS6 CR1 has a lot of ClassLoading issues => even moving EJB JAR to WAR didn't help => disable EJB completely for this moment
// - also change app-prod-service.xml to lookup for EJB's JNDI again...
// ##################################################
@Stateless(name = "addressServiceEJB", mappedName="addressServiceEJB")
@Remote
// ##################################################
// Note: SpringBeanAutowiringInterceptor requires beanRefContext.xml to be defined (on EAR level)
// ##################################################
@Interceptors(SpringBeanAutowiringInterceptor.class)
public class AddressServiceImpl extends AbstractWrapperServiceImpl<Address> implements AbstractWrapperService<Address>
{
	Logger logger = Logger.getLogger(AddressServiceImpl.class.getName());
	
	@Autowired AddressDao addressDao;
	
	@Override
	public Dao<Address> getDao()
	{
		return addressDao;
	}

	@Override
	@Transactional(timeout=30, propagation=Propagation.REQUIRED, readOnly=true)
	public boolean canBeRemoved(Address entity)
	{
		if (entity == null)
			return true;

		entity = getDao().getById(entity.getId());
		if (entity == null)
			return true;

		// 5.try - disable lazy-loading completely (not recommended due to performance and memory allocation reasons!)

		return entity.canBeRemoved();
	}

}
